package com.cls.business.strategy.impl.notice;

import com.cls.business.entity.Notice;
import com.cls.business.entity.Standard;
import com.cls.business.entity.TopicPlan;
import com.cls.business.service.INoticeService;
import com.cls.business.service.IStandardService;
import com.cls.business.service.ITopicPlanService;
import com.cls.business.strategy.NoticeStrategy;
import com.cls.common.enums.DictEnum;
import com.cls.common.exception.MyException;
import com.cls.system.entity.Configure;
import com.cls.system.service.IConfigureService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;

/**
 * 课题征集的实现方法
 *
 * @author Administrator
 */
@Component("notice_type_ktzj")
@RequiredArgsConstructor
public class KTZJNoticeImpl implements NoticeStrategy {

    private final INoticeService noticeService;

    private final ITopicPlanService topicPlanService;



    @Override
    @Transactional
    public void createNotice(Notice notice) {
        noticeService.save(notice);
        TopicPlan topicPlan = new TopicPlan();
        topicPlan.setSelectNoticeId(notice.getNoticeId());
        topicPlan.setSelectBeginTime(notice.getSelectBeginTime());
        topicPlan.setIsGenerateCode(false);
        if (StringUtils.isNotEmpty(notice.getYear())) {
            topicPlan.setYear(notice.getYear());
            topicPlan.setPeriod(notice.getYear());
        } else {
            Calendar date = Calendar.getInstance();
            String year = date.get(Calendar.YEAR) + "";
            topicPlan.setYear(year);
            topicPlan.setPeriod(year);
        }
        TopicPlan topicPlan1 = topicPlanService.selectAnnualByYear(topicPlan.getYear());
        if (topicPlan1 != null) {
            throw new MyException("对不起，当前年份的课题征集已创建！");
        }
        topicPlan.setSelectEndTime(notice.getSelectEndTime());
        topicPlan.setStatusCode(DictEnum.PLAN_CREATE.getCode());
        //设置计划为年度课题
        topicPlan.setTopicClassifyCode(DictEnum.ANNUAL.getCode());
        topicPlanService.save(topicPlan);
    }
}
